﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using nwrole.plugin;

namespace nwrole.lib {
    public class TestPluginProvider : PluginProviderBase {

        //private string serviceName;

        [System.ComponentModel.Composition.ImportMany]
        IEnumerable<IPlugin> Plugins { get; set; }

        #region IPluginProvider

        public override PropertyList LoadPluginSettings() {
            PropertyList plist = null;
            if (File.Exists(PluginConfigPath)) {
                plist = new PropertyList(PluginConfigPath);
            }
            return plist;
        }

        public override void SavePluginSettings(PropertyList plist) {

            plist.Save(PluginConfigPath);
        }

        public override void RefreshPlugins() {
            var catalog = new AggregateCatalog();
            var container = new CompositionContainer(catalog);
            var batch = new CompositionBatch();
            batch.AddPart(this);
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new DirectoryCatalog(ServicePath));
            container.Compose(batch);
        }

        #endregion

        private string ServicePath {
            get {
                return Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
            }
        }
        private string PluginConfigPath {
            get {
                return Path.Combine(ServicePath, "plugin-config.xml");
            }
        }

        private string ServiceConfigPath {
            get {
                return Path.Combine(ServicePath, "service-config3.xml");
            }
        }
    }
}
